﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pavilion.GameTools;
using System.Xml.Linq;

namespace Pavilion.Locks
{
    public class SpeedXLock : ILock
    {
        public float SpeedX { get; set; }
        public Operand Operand { get; set; }
        public string GameItemName { get; set; }

        public ILock Clone()
        {
            return new SpeedXLock()
            {
                GameItemName = this.GameItemName,
                SpeedX = this.SpeedX,
                Operand = this.Operand
            };
        }
        
        public void Deserialize(XElement lockElement, string resourceName)
        {
            SpeedX = float.Parse(lockElement.Attribute("SpeedX").Value);
            Operand = (Operand)Enum.Parse(typeof(Operand), lockElement.Attribute("Operand").Value);
        }

        public XElement Serialize()
        {
            XElement lockElement = new XElement("Lock");

            lockElement.Add(new XAttribute("SpeedX", SpeedX));
            lockElement.Add(new XAttribute("Operand", Operand));

            return lockElement;
        }

        public LockMonitorBase CreateLockMonitor(GameItem gameItem)
        {
            PhysicalTypeTool physicalTool = gameItem.ObtainTool<PhysicalTypeTool>();

            if (Operand == Operand.GreaterThen)
                return new LockPredicateMonitor(physicalTool, () => physicalTool.SpeedX < SpeedX, this);
            else if (Operand == Operand.LessThen)
                return new LockPredicateMonitor(physicalTool, () => physicalTool.SpeedX > SpeedX, this);
            else
                return new LockPredicateMonitor(physicalTool, () => physicalTool.SpeedX == SpeedX, this);
        }

        public override string ToString()
        {
            return this.GetType().Name;
        }
        }
    }

    public enum Operand
    {
        GreaterThen,
        LessThen,
        EqualTo,
    }

